package com.matt.docpro.controller;

import cn.hutool.core.date.DateUtil;
import com.matt.docpro.config.Utils;
import com.matt.docpro.model.Feedback;
import com.matt.docpro.model.User;
import com.matt.docpro.service.FeedbackService;
import com.matt.docpro.service.LoginInfoService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * FeedbackController
 * Created by on 2024/03/05.
 */
@Controller
public class FeedbackController {

    @Autowired
    LoginInfoService loginInfoService;
    @Autowired
    FeedbackService feedbackService;

    @RequestMapping("/feedback/submit")
    public String init(Model model, HttpServletRequest request) throws Exception {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        model.addAttribute("user", user);
        return "submit";
    }

    @PostMapping("/feedback/submit/save")
    @ResponseBody
    public Map<String, Object> adminUsersDetailSave(Model model, Feedback feedback, HttpServletRequest request) {
        Map<String, Object> res = new HashMap<>();
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        feedback.setId(System.currentTimeMillis()+"");
        feedback.setTime(DateUtil.date());
        feedback.setUserId(user.getId());
        feedback.setUserNm(user.getName());
        feedbackService.insert(feedback);
        res.put("state", "success");
        return res;
    }
}
